﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kovan.DAL.UserCenter;
using Kovan.Model.Entity.UserCenter;
using Kovan.Model.Parameter.UserCenter;
using Kovan.Model.ViewModel;
using Kovan.Model.Common;
using Kovan.Model.ViewModel.UserCenter;

namespace Kovan.BLL.UserCenter
{
    public class BActModule
    {
        DActModule dal = new DActModule();

        #region 当前类的实例
        private static readonly BActModule Instances = new BActModule();

        private BActModule() { }

        public static BActModule GetInstances()
        {
            return Instances;
        }
        #endregion


        public List<act_module> GetList(int applicationid)
        {
            return dal.GetList(applicationid);
        }

        public IEnumerable<act_module> GetList(PModule param)
        {
            return dal.GetList(param);
        }

        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public act_module GetModel(long id)
        {
            return dal.GetModel(id);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        public int Add(act_module model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public int Delete(long id)
        {
            return dal.Delete(id);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids">ID集合</param>
        /// <returns></returns>
        public int BatchDelete(IEnumerable<string> ids)
        {
            return dal.BatchDelete(ids);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        public int Update(act_module model)
        {
            return dal.Update(model);
        }

        public IEnumerable<EMenu> LoadMenu(PModule param, List<EMenu> list, int level = 0)
        {
            string line = "----------------------------------------";
            if (!param.parent_id.HasValue)
            {
                return null;
            }
            var elist = GetList(param);

            foreach (var item in elist)
            {
                EMenu model = new EMenu();
                model.module = item;
                if (level == 0)
                {
                    model.name2 = item.name;
                }
                else
                {
                    model.name2 = "|" + line.Substring(0, level + 1) + item.name;
                }
                list.Add(model);
                PModule p = new PModule { application_id = param.application_id.Value, parent_id = item.module_id.Value };
                var sublist = GetList(p);
                if (sublist != null && sublist.Count() > 0)
                {
                    LoadMenu(p, list, level + 1);
                }
            }
            return list;
        }

        //public IEnumerable<Menu> LoadMenu(PModule param, List<EMenu> list)
        //{
        //    return null;
        //}

        public List<string> GetSonModuleIds(long module_id, IEnumerable<act_module> elist)
        {
            var ids = new List<string>();
            var list1 = elist.Where(c => c.parent_id == module_id);
            if (list1 != null)
            {
                foreach (var item in list1)
                {
                    ids.Add(item.module_id.ToString());
                    var list2 = elist.Where(c => c.parent_id == item.module_id);
                    if (list2 != null)
                    {
                        ids.AddRange(GetSonModuleIds(item.module_id.Value, elist));
                    }
                }
            }
            return ids;
        }

        public IEnumerable<EModuleAuthority> GetModuleAuthority(PModule param)
        {
            return dal.GetModuleAuthority(param);
        }
    }
}
